package com.feihao.zhou.service;

import com.feihao.zhou.bean.Student;
import com.feihao.zhou.bean.User;
import com.feihao.zhou.mapper.StudentMapper;
import com.feihao.zhou.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserService {

    public User checkOutUser(User user) {

        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);

            User user1 = mapper.checkOutUser(user);
            is.close();
            sqlSession.close();
            return user1;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Student> selectAllStudent() {
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            List<Student> list = mapper.selectAllStudent();
            is.close();
            sqlSession.close();
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Student> selectStudentByName(String name) {
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            List<Student> list = mapper.selectStudentByName(name);
            is.close();
            sqlSession.close();
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Student> selectStudentByAge(String age) {
        try {
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            List<Student> list = mapper.selectStudentByAge(age);
            is.close();
            sqlSession.close();
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
